package de.bazinga.rotw.menu;

import com.jme.animation.SpatialTransformer;
import com.jme.image.Texture;
import com.jme.input.MouseInput;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jmex.game.state.GameStateManager;

import de.bazinga.rotw.game.Game;
import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.sound.Sound;

public class SplashScreen extends Menu {

	public SplashScreen() {
		super("splashMenu");
	}
	
	
	protected void init() {		
		// Background
		Quad bgQuad = new Quad("bg",
				Game.getGame().getDisplay().getWidth(), 
				Game.getGame().getDisplay().getHeight());
		
		bgQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO);   
	 	
		bgQuad.setLocalTranslation(new Vector3f(
				Game.getGame().getDisplay().getWidth()/2,
				Game.getGame().getDisplay().getHeight()/2,
				0));
		
		TextureState bg = Game.getGame().getDisplay().getRenderer().createTextureState();
		bg.setEnabled(true);
		bg.setTexture(TextureManager.loadTexture(RotwGameState.class
				.getClassLoader().getResource("splash/splashBg.png"),
				Texture.MinificationFilter.BilinearNearestMipMap,
				Texture.MagnificationFilter.Bilinear));
		
		bgQuad.setRenderState(bg);
		
		bgQuad.setLightCombineMode(Spatial.LightCombineMode.Off);
		bgQuad.updateRenderState();
		
		getRootNode().attachChild(bgQuad);
		
		
		Quad iconQuad = new Quad("bg",
				300, 
				333);
		
		iconQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO);   
	 	
		iconQuad.setLocalTranslation(new Vector3f(
				Game.getGame().getDisplay().getWidth()/2,
				Game.getGame().getDisplay().getHeight()/2,
				0));
		
		TextureState icon = Game.getGame().getDisplay().getRenderer().createTextureState();
		icon.setEnabled(true);
		icon.setTexture(TextureManager.loadTexture(RotwGameState.class
				.getClassLoader().getResource("splash/splashIcon.png"),
				Texture.MinificationFilter.BilinearNearestMipMap,
				Texture.MagnificationFilter.Bilinear));
		
		iconQuad.setRenderState(icon);
		
		iconQuad.setLightCombineMode(Spatial.LightCombineMode.Off);
		iconQuad.updateRenderState();
		
		
		SpatialTransformer st = new SpatialTransformer(1);
		st.setObject(iconQuad, 0, -1);
		st.setRepeatType(SpatialTransformer.RT_CYCLE);
		st.setSpeed(0.8f);
		
		st.setScale(0, 0, new Vector3f(1,1,1));
		st.setScale(0, 1, new Vector3f(0.7f,0.7f,0.7f));
		st.setScale(0, 2, new Vector3f(0.4f,0.4f,0.4f));
		st.setScale(0, 3, new Vector3f(0.7f,0.7f,0.7f));
		st.setScale(0, 4, new Vector3f(1,1,1));
		
		
		st.interpolateMissing();
		iconQuad.addController(st);
		
		getRootNode().attachChild(iconQuad);
	}
	
	
	protected void initMenuItems() {
		// We dont need any menuItems
	}
	
	public void update(float tpf) {
		super.update(tpf);		
		
		if(MouseInput.get().isButtonDown(0)) {

			if(Game.getGame().getSettings().isSFX()) {
				Sound.getInstance().addSound("sounds/bazinga.wav");
				Sound.getInstance().getAudioTrack("sounds/bazinga.wav").play();
			}
			
			GameStateManager.getInstance().getChild("mainMenu").setActive(true);
			this.setActive(false);
		}
	}
}
